package com.jnshu.controller;

import com.jnshu.entity.Student;
import com.jnshu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping( "/stu")
public class StuController {
    @Autowired
    private StudentService studentService;

    /**
     * 获取所有用户列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getAllUser")
    public String getAll(HttpServletRequest request, Model model) {
        List<Student> students = studentService.findAll();
        model.addAttribute(students);
        request.setAttribute("students", students);
        return "allUser";
    }

    //    添加用户界面
    @RequestMapping("/toAddUser")
    public String toAddUser() {
        return "addUser";
    }

    //       /**
//         * 添加用户并重定向
//         * @param user
//         * @param request
//         * @return
//         */
    @RequestMapping("/addUser")
    public String addUser(Student student, Model model) {
        studentService.save(student);
        return "redirect:/stu/getAllUser";
    }

    /**
     * 编辑用户
     *
     * @param student
     * @param request
     * @return
     */
    @RequestMapping("/updateUser")
    public String updateUser(Student student, HttpServletRequest request, Model model) {
        if (studentService.updateUser(student) != 0) {
            student = studentService.findUserById(student.getSid());
            request.setAttribute("student", student);
            model.addAttribute("student", student);
            return "redirect:stu/getAllUser";
        } else {
            return "error";
        }
    }
    /**
     * 根据id查询单个用户
     * @param sid
     * @param request
     * @return
     */
    @RequestMapping("/getUser")
    public String getUser(Integer sid,HttpServletRequest request,Model model){
        request.setAttribute("student",studentService.findUserById(sid));
        model.addAttribute("student",studentService.findUserById(sid));
        return "editUser";
    }
    /**
     * 删除用户
     * @param sid
     * @param request
     * @param response
     */
    @RequestMapping("/delUser")
    public void delUser(Integer sid,HttpServletRequest request,HttpServletResponse response){
        String result="{\"result\":\"error\"}";
        if(studentService.deleteUserById(sid))
        {
            result="{\"result\":\"success\"}";
        }else {
            result="{\"result\":\"error\"}";
        }
        response.setContentType("application/json");
        try {
            PrintWriter out=response.getWriter();
            out.write(result);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }





}


